var fieldJobId;
var changeFromClick = false;
// var reBackNodeText;
// 初始化数据
var parameterData = [ {
	valueCode : '0',
	valueName : '默认参数'
}, {
	valueCode : '1',
	valueName : '自定义参数'
} ];
// 定位显示
var arrayLi = [];
var arrayLiInd = 0;
$(document).ready(function() {
					$(".right_con").css("display", "block");
					$(".right_con_second").css("display", "none");
					$("#divDatagrid").show();
					$("#divTextArea").hide();
					$("#ff input").val("");
					$("#ff :input").bind('change', function() {
						$("#ff").data("changed", true);
					});

					// 领域系统结构树
					var tree_id = '';
					var isSave = false;
					var modifyNode;

					// var reBackNode;
					$("#tt").tree({
						url : BASE_URL_TREE
						+ '/getHttp4User4EasyUiTree'
						+ '?v=' + Math.random(),
						lines : false,
						onLoadSuccess : function() {
							$(
									"#tt > li > ul > li > .tree-node > .tree-file, #tt > li > .tree-node > .tree-file")
									.css("background",
											"url('../../../images/st_folder.png') 2px 2px no-repeat");
							
							var nodes = $('#tt').tree('getChildren');
							function changeNodeClass(nodelist, start, end) {

								if(start < nodelist.length)
								{
									for(var i = start; i <= end; i++)
									{
										var node = nodelist[i];
										if(node.nodeType == "fieldSystem" && node.hasChildren && (!node.children || node.children.length == 0) && $(node.target).find(".tree-fieldSys").length == 0 )
										{
											$(node.target).find("span.tree-icon").prev().remove();
											$(node.target).children(".tree-icon.tree-file").before('<span class="tree-fieldSys tree-collapsed" data-id="' + node.id + '"></span>');
											$(node.target).children(".tree-fieldSys").click(function(){

												var $self = $(this);
												if($self.hasClass('tree-collapsed'))
												{//alert(node.id+'  '+node.text);													
													$self.removeClass('tree-collapsed').addClass('tree-expanded');
													//$('#tt').tree('expend', $self.parent()[0])
													$.ajax({
														url :BASE_URL_TREE+ '/getHttpBySystemId?systemId=' + $self.attr("data-id") + '&operationType=1',
														dataType : 'json',
														type : 'get',
														success : function(data){
															//alert(data)
															$('#tt').tree('append',{
																parent: $self.parent()[0],
																data: data
															});
														}
													}); 
												}
												else
												{
													$self.addClass('tree-collapsed').removeClass('tree-expanded');
													$('#tt').tree('collapse', $self.parent()[0]);
												}
												
											});
										}
										var icons = $(node.target).find("span.tree-icon");
										if (node.nodeType == 'systemInterface') {
											//var icons = $(node.target).find("span.tree-icon");
											if(node.interfaceType=='1'){
												$(icons[0]).removeClass("tree-file");
												$(icons[0]).addClass("icons4WebService");
											}
											else if(node.interfaceType=='2'){
												$(icons[0]).removeClass("tree-file");
												$(icons[0]).addClass("icons4Rest");
											}
											else if(node.interfaceType=='3'){
												$(icons[0]).removeClass("tree-file");
												$(icons[0]).addClass("icons4Ftp");
											}
										}
									}
									if(end + 1 < nodelist.length)
									{
										start = end + 1;
										end += 100;
										end = end > nodelist.length - 1? nodelist.length - 1:end;
										setTimeout(function(){
											changeNodeClass(nodelist, start, end);
										}, 50);
									}
								}
							}
							var end = nodes.length > 100? 99:nodes.length;
							changeNodeClass(nodes, 0, end);
						},
						onBeforeSelect : function(node) {
							if ($(node.target).children(
									".tree-folder").length) {
								return false;
							}
							if ($(node.target).children(
									".tree-file").length
									&& ($(node.target).parent()
											.parent().parent()
											.parent()[0].id == "tt" || $(
											node.target)
											.parent().parent()[0].id == "tt")) {
								return false;
							}
							return true;
						},
						onClick : function(node) {
							arrayLi = [];
							arrayLiInd = 0;
							changeFromClick = true;
							$("#systemName").val(node.text);
						},
						onSelect : function(node) {
							/** ***************************************************** */
							$("#ff").data("changed", false);
							if (node.nodeType == 'systemInterface') {
								// $(".right_mask").css("display","none");
								fieldJobId = node.id;
								if (tree_id != fieldJobId) {
									tree_id = fieldJobId;
									$("#ff input").val("");
								} else {
									return false;
								}
								var interfaceId;
								$.ajax({
									url : '/api/tokens/getRoutes'+ '?interfaceId='+ node.id,
									dataType : 'json',
									type : 'get',
									// contentType :
									// "application/json",
									success : function(data) {
										$(".middle").show();
										$("#interfaceName").text("接口名称："+data.interfaceName);
										$("#interfaceType").text("接口类型："+data.interfaceType);
										var apphtml='';
										var pubhtml='';
										if(data.appSystems.length>0){
											for(var i=0;i<data.appSystems.length;i++){
												apphtml+='<li><div class="img_icon"><img src="../../../images/appESB.jpg" alt="" /><p>'+data.appSystems[i]+'</p></div><div class="small_line"></div></li>';
											}
											$("#jiantou").css("visibility","visible");
											$("#appSystem").html(apphtml);
										}else{
											apphtml='<li><div class="img_icon" style="display:none"><img src="../../../images/appESB.jpg" alt="" /><p style="color:red">接口无调用方</p></div><div class="small_line" id="jiantou"></div></li>';
											$("#appSystem").html(apphtml);
											$("#jiantou").css("visibility","hidden");
										}
										
										if(data.pubSystems.length>0){
											for(var i=0;i<data.pubSystems.length;i++){
												pubhtml+='<li><div class="small_line"></div><div class="img_icon"><img src="../../../images/pubESB.jpg" alt="" /><p>'+data.pubSystems[i]+'</p></div></li>';
											}
											$("#pubSystem").html(pubhtml);
										}else{
											pubhtml+='<li><div class="small_line"></div><div class="img_icon"><img src="../../../images/pubESB.jpg" alt="" /><p>发布方</p></div></li>';
											$("#pubSystem").html(pubhtml);
											
										}
										/*if(data.length!=0){
											$(".right").show();
											var apphtml ='';
											var size = data.appSystems.lenth;
											alert(size);
											for(var i=0;i<size;i++){
												apphtml+='<li><div class="img_icon"><img src="../../../images/appESB.jpg" alt=""/><p>'+data[i].appSystemName+'</p></div><div class="small_line"></div></li>'
											}
											$("#appSystem").html(html);
										}else{
											$(".right").show();
											//$("#esbpingtai").css("visibility","hidden");
											var html2 ='<li><div class="img_icon"><img src="../../../images/appESB.jpg" alt=""/><p>'+"此接口没有调用方"+'</p></div><div class="small_line" style="background:white"></div></li>';
											$("#appSystem").html(html2);
										}*/
										$(".left_menu, .right_con").css("height",
												($("body").height() - 25) + "px");
										$(".tree_con").css("height",
												($("body").height() - 95) + "px");

										$(".left_menu, .right_con_second").css("height",
												($("body").height() - 25) + "px");
										$(".tree_con").css("height",
												($("body").height() - 95) + "px");
										$(".right_content").height(
												$(".left_menu").outerHeight());
										$(".content").css("display", "block");
										$(".small_line").css(
												"margin-top",
												($(".img_icon").outerHeight() - $(
														".small_line").outerHeight()) / 2);
										var marginL = ($(".right_content").outerWidth() - $(
												".content").outerWidth()) / 2;
										if (marginL < 0) {
											marginL = 10;
										}
										if ($(".right_content").outerWidth() < $(document)
												.outerWidth()) {
											$(".middle")
													.css(
															{
																"position" : "fixed",
																"top" : ($(".right_content")
																		.outerHeight() - $(
																		".middle")
																		.outerHeight()) / 2 + 38,
																"left" : $(".left")
																		.outerWidth()
																		+ marginL
																		+ $(".left_menu")
																				.outerWidth()
																		+ 29 + "px"
															});
										} else {
											$(".middle")
													.css(
															{
																"position" : "fixed",
																"top" : ($(".right_content")
																		.outerHeight() - $(
																		".middle")
																		.outerHeight()) / 2 + 38,
																"left" : $(".left")
																		.outerWidth()
																		+ marginL
																		+ $(".left_menu")
																				.outerWidth()
																		+ 29 + "px"
															});
										}

										if ($(".right_content").outerHeight() < $(".left")
												.outerHeight()) {
											//$(".content").outerHeight($(".right_content").outerHeight());
											var diffH = ($(".content").outerHeight()
													- $(".left").outerHeight() - 40) / 2;
											if (diffH < 0) {
												diffH = 40;
											}
											if ($(".right_content").outerHeight() > $(
													".right").outerHeight()) {
												$(".left").css({
													"top" : diffH
												});
											} else {
												$(".rileftght").css({
													"top" : 0
												});
											}
										} else {
											$(".left").css(
													{
														"position" : "fixed",
														"top" : ($(".right_content")
																.outerHeight() - $(".left")
																.outerHeight()) / 2 + 38,
														"left" : marginL
																+ $(".left_menu")
																		.outerWidth()
																+ "px"
													});
										}
										if ($(".right_content").outerHeight() < $(".right")
												.outerHeight()) {
											var diffH = ($(".content").outerHeight()
													- $(".right").outerHeight() - 40) / 2;
											if (diffH < 0) {
												diffH = 40;
											}
											$(".right").removeAttr("style");
											if ($(".right_content").outerHeight() > $(
													".left").outerHeight()) {
												$(".right").css({
													"top" : diffH,
													"left" : $(".left").outerWidth() + "px"
												});
											} else {
												if ($(".left").css("position") == "fixed") {
													$(".right").css({
														"top" : 0
													});
												} else {
													if ($(".left").outerHeight() > $(
															".right").outerHeight()) {
														$(".left")
																.css(
																		{
																			"top" : ($(
																					".content")
																					.outerHeight()
																					- $(
																							".left")
																							.outerHeight() - 40) / 2
																		});
														$(".right")
																.css(
																		{
																			"top" : ($(
																					".content")
																					.outerHeight()
																					- $(
																							".right")
																							.outerHeight() - 40) / 2
																		});
													} else {
														$(".left")
																.css(
																		{
																			"top" : ($(
																					".content")
																					.outerHeight()
																					- $(
																							".left")
																							.outerHeight() - 40) / 2
																		});
														$(".right").css({
															"top" : diffH
														});

													}

												}

											}
										} else {
											$(".right")
													.css(
															{
																"position" : "fixed",
																"top" : ($(".right_content")
																		.outerHeight() - $(
																		".right")
																		.outerHeight()) / 2 + 38,
																"left" : $(".left")
																		.outerWidth()
																		+ marginL
																		+ $(".left_menu")
																				.outerWidth()
																		+ "px"
															});
										}

										if ($(".right_content").outerHeight() > $(
												".content").outerHeight()) {
											$(".content").outerHeight(
													$(".right_content").outerHeight());
										}

										$(".line").css(
												"margin-top",
												($(".right").outerHeight() - $(".line")
														.outerHeight()) / 2);
										
									},
									error : function() {
										$.messager.alert("错误","<p>服务异常，查询接口信息失败，请重新登录或稍后再试！ </p>","error");
									}
								});
							}
						}
					});

					$("#searchNode").click(function (e) { 
						if($("#systemName").val() != '')
						{
							//后台查询匹配的接口
							$.ajax({
								url : BASE_URL_HTTP_INTERFACE + '/findHttpInterfaceByIfName' + '?ifName=' + encodeURI($("#systemName").val()) + '&v=' + Math.random(),
								dataType : 'json',
								type : 'get',
								success : function(data) {
									if(data.backValue == true){
										var system = data.httpDatas;
										if(system.indexOf(",") != -1){
											var systems = system.split(",");
										}else{
											var systems = [system];
										}
										resultSysNum = systems.length;
										loadSysNum = 0;
										for(var i=0; i < systems.length; i++){
											//展开系统的节点
											//找到树的所有节点
											var nodes = $('#tt').tree('getChildren');
											for(var j = 0; j <= nodes.length; j++)
											{
												if(nodes[j].text == systems[i]){
													var $self = $(nodes[j].target).find("span.tree-fieldSys");
													if($self.length)
													{	
														 expendNode(nodes[j]);
													}
													else
													{
														loadSysNum++;
														if(loadSysNum >= resultSysNum)
														{
															choose_tree();
														}
													}
													break;
												}
											}
											
										}
									
									}else{
										$.messager.alert("错误","<p>没有找到包含  " + $("#systemName").val() + " 的接口！ </p>","error");
									}
								},
								error : function() {
									$.messager.alert("错误","<p>服务异常，查询接口信息失败，请重新登录或稍后再试！ </p>","error");
								}
							});
						}else
						{
							//搜索关键字为空时，不进行搜索
							/*$("input").val("");
							var row = $('#tt li .tree-node');  
							row.removeClass('tree-node-selected');*/
							$(".tree_con").scrollTop(0);
						}
					});
					$("#systemName").bind('input propertychange', function() {
						if (changeFromClick) {
							changeFromClick = false;
							return;
						}
						arrayLi = [];
						arrayLiInd = 0;
						if ($("#systemName").val() != '') {
//							choose_tree();
						} else { // 搜索关键字为空时，不进行搜索
							/*
							 * $("#ff input").val(""); var row = $('#tt li
							 * .tree-node');
							 * row.removeClass('tree-node-selected');
							 */
							$(".tree_con").scrollTop(0);
						}
					});
					function expendNode(node) {
						var $self = $(node.target).find("span.tree-collapsed");
						if(!$self.length)
						{
							return;
						}
						$self.removeClass('tree-collapsed').addClass('tree-expanded');
						$.ajax({
							url :BASE_URL_TREE+ '/getHttpBySystemIdForScan?systemId=' + node.id,
							dataType : 'json',
							type : 'get',
							success : function(data){
								//alert(data)
								$('#tt').tree('append',{
									parent: $self.parent()[0],
									data: data
								});
								loadSysNum++;
								if(loadSysNum >= resultSysNum)
								{
									choose_tree();
								}
							},
							error: function() {
								loadSysNum++;
								if(loadSysNum >= resultSysNum)
								{
									choose_tree();
								}
							}
						}); 
					}
					$(document, "#systemName").keyup(function(event) {
						if (event.keyCode == 13) {
							$("#searchNode").trigger("click");
						}
					});

					function getSelected() {
						var node = $('#tt').tree('getSelected');
						if (node.nodeType == 'systemInterface') {
							$.ajax({
								url : BASE_URL_BASE_URL_HTTP_INTERFACE_CRUD+ '/'+ node.id+ '?v='+ Math.random(),
								dataType : 'json',
								type : 'get',
								contentType : "application/json",
								success : function(data) {
									FormEcho(data);
								},
								error : function() {
									alert("error");
								}
							});
						}
					}

					function choose_tree() {
						$('#tt').tree('expandAll');
						if (arrayLi.length == 0 && arrayLiInd == 0) {
							var searchVal = $("#systemName").val()
									.toUpperCase();
							var row = $('#tt li .tree-node');
							var rowcolumn = $('#tt li .tree-node > .tree-title');
							var i = 0, j = 0;

							// row.removeClass('tree-node-selected');
							row.removeClass('tree-node-hover');
							rowcolumn.each(function(column) {
								var matchVal = $(this).text();
								if (matchVal.indexOf(searchVal) != -1) {
									i++;
									var scrTop = $(".tree_con").scrollTop()+ $(this).offset().top
									- parseInt($(".tree_con").css("padding-top"),10)
									- $(".search-con")[0].offsetHeight
									- $(".left_menu > .menu_tit")[0].offsetHeight;
									if (i <= 1) {
										// $(this).parent().addClass('tree-node-selected');
									$(this).parent().addClass('tree-node-hover'); // 改成鼠标hover状态，不选中搜索结果
									$(".tree_con").scrollTop(scrTop);
									arrayLiInd = 1;
									// getSelected();
									}
										arrayLi.push([ matchVal, scrTop ]);
										}
									});
						} else {
							var rowcolumn = $('#tt li .tree-node > .tree-title');
							var row = $('#tt li .tree-node');
							// row.removeClass('tree-node-selected');
							row.removeClass('tree-node-hover');
							if (arrayLiInd < arrayLi.length) {
								rowcolumn.each(function(column) {
									var matchVal = $(this).text();
									if (matchVal == arrayLi[arrayLiInd][0]) {
										// $(this).parent().addClass('tree-node-selected');
										$(this).parent().addClass(
												'tree-node-hover'); // 改成鼠标hover状态，不选中搜索结果
										$(".tree_con").scrollTop(
												arrayLi[arrayLiInd][1]);
										arrayLiInd++;
										// getSelected();
										return false;
									}
								});
							} else {
								// arrayLi = [];
								arrayLiInd = 0;
								$(".tree_con").scrollTop(0);
								choose_tree();
							}
						}
					}
				});
